package com.example.backend.controller.goods;

import cn.hutool.core.convert.Convert;
import com.example.backend.dto.request.OrderItemDto;
import com.example.backend.entitly.mysql.order.OrderItems;
import com.example.backend.entitly.mysql.order.ProductSpecs;
import com.example.backend.service.mysql.order.OrderItemsService;
import com.example.backend.service.mysql.permission.ProductSpecsService;
import com.example.backend.utils.BigDecimalUtils;
import com.example.backend.utils.ResultJson;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/order")
@Tag(name = "订单项管理")
public class OrderGoodsController {

    private final OrderItemsService OrderItemsService;
    private final ProductSpecsService productSpecsService;

    @Operation(summary = "创建订单项", description = "创建订单项", tags = {"订单项管理"})
    @PreAuthorize("hasAnyAuthority('order:create')")
    @PostMapping("/create")
    public ResultJson<?> createOrder(@RequestBody List<OrderItemDto> orderItems) {
        log.debug("创建订单项: {}", orderItems.toString());
        List<OrderItems> collect = orderItems.stream().map(item -> {
            return Convert.convert(OrderItems.class, item);
        }).collect(Collectors.toList());
        List<OrderItems> newOrderItems = new ArrayList<>();
        for (OrderItems orderItem : collect) {
            OrderItems order = OrderItemsService.getById(orderItem.getProductId());
            ProductSpecs productSpecs = productSpecsService.getById(orderItem.getSpecId());
            orderItem.setProductName(order.getProductName());
            orderItem.setProductImage(order.getProductImage());
            orderItem.setSpecInfo(productSpecs.getSpecName());
            orderItem.setPrice(order.getPrice());
            orderItem.setTotalPrice(BigDecimalUtils.multiply(order.getPrice(), orderItem.getQuantity()));
            OrderItemsService.save(orderItem);
            newOrderItems.add(orderItem);
        }
        return ResultJson.success(newOrderItems);
    }

    @Operation(summary = "取消订单", description = "取消订单", tags = {"订单管理"})
    @PreAuthorize("hasAnyAuthority('order:cancel')")
    @PostMapping("/cancel")
    public ResultJson<?> cancelOrder() {
        return null;
    }

    @Operation(summary = "支付订单", description = "支付订单", tags = {"订单管理"})
    @PreAuthorize("hasAnyAuthority('order:pay')")
    @PostMapping("/pay")
    public ResultJson<?> payOrder() {
        return null;
    }

    @Operation(summary = "查询订单", description = "查询订单", tags = {"订单管理"})
    @PreAuthorize("hasAnyAuthority('order:query')")
    @PostMapping("/query")
    public ResultJson<?> queryOrder() {
        return null;
    }

    @Operation(summary = "删除订单", description = "删除订单", tags = {"订单管理"})
    @PreAuthorize("hasAnyAuthority('order:delete')")
    @DeleteMapping("/delete")
    public ResultJson<?> deleteOrder() {
        return null;
    }

    @Operation(summary = "修改订单", description = "修改订单", tags = {"订单管理"})
    @PreAuthorize("hasAnyAuthority('order:update')")
    @PostMapping("/update")
    public ResultJson<?> updateOrder() {
        return null;
    }
}
